$(function(){
    /*
     * 加载列表数据
     * */
    $('#listGrid').datagrid({
        url:ROOT_PATH+'/risk/statistics/statisticsByRecogniseType.shtml',
        queryParams:{
           // sort:'addTime',
            //order:'desc'
        },
        //idField:'id',
        fitColumns:true,
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        sortOrder:true,
        striped:true,
        fit:true,
        columns:[[
            //{field:'id',width:10,checkbox:true},
            {field:'deptId',title:'部门Id',width:10,hidden:true},
            {field:'deptName',title:'部门名称',width:10},
            {field:'recogniseType0',title:'设备设施',width:10},
            {field:'recogniseType1',title:'作业活动',width:10},
            {field:'riskNum',title:'风险总数',width:10}

        ]],
        toolbar: '#toolbar'
    });

    /**
     * 加载下拉列表信息
     *
     */
    queryRegionList();//风险区域
    queryDepartmentTree();//部门下拉列表
    //加载评价方法
    queryMethodTypeList("search_methodTypeId",CONSTANT.DictionaryTypeDefConstants.RISK_RECOGNISE_MOTHOD);//评价方法
    $("#search_recogniseUser").textbox({
        iconWidth: 22,
        icons: [{
            iconCls: 'icon-add',
            handler: function(e){
                selectUserId ="search_recogniseUser";
                add_contractorInfo_func();
            }
        },
            {
                iconCls: 'icon-remove',
                handler: function(e){
                    $(e.data.target).textbox('clear');

                }
            }
        ]
    });
    //绑定人员选择按钮事件
    $("#searchAddUser").textbox({
        iconWidth: 22,
        icons: [
            {
                iconCls: 'icon-add',
                handler: function(e){
                    selectUserId ="searchAddUser";
                    add_contractorInfo_func();
                }
            },
            {
                iconCls: 'icon-remove',
                handler: function(e){
                    $(e.data.target).textbox('clear');

                }
            }
        ]
    });



    /**
     * 绑定按钮事件
     */
    $('#btn_advanced_search').on("click", function () {
        $('#searchForm2').toggle();
        $('#listGrid').datagrid('resize');
    });
    //查询按钮绑定事件
    $("#btn_search").on('click',function(){
        searchFun();
    });
    $('#btn_clear').on('click',function(){
        $('#searchForm1').form('clear');
        $('#searchForm2').form('clear');
    });
});
/**
 * 查询按钮方法
 */
function searchFun(){
    var search1 =$('#searchForm1').serializeJson();
    var search2 =$('#searchForm2').serializeJson();
    var search = $.extend(search1,search2);
    //排序参数
    //search.sort = "addTime";
    //search.order='desc';
    $('#listGrid').datagrid('load',search);
}
/**
 * 查询风险区域列表
 */
function queryRegionList(){
    $.ajax({
        url:ROOT_PATH + '/risk/region/queryComboboxList.shtml',//提交地址
        data:{},
        type:"POST",
        dataType:"json",
        success:function(result){
            $('#search_regionId').combobox(
                {
                    data:result,
                    valueField:'id',
                    textField:'text',
                    //limitToList:'true',
                    /*loadFilter:function(data){
                     var newData = new Array();
                     newData.push({id:"",text:'请选择'});
                     for(var i=0;i<data.length;i++){
                     newData.push(data[i])
                     }
                     return newData;
                     },*/
                    //hasDownArrow:false,//隐藏倒三角
                }
            );

        }
    });
}
/**
 * 查询部门树方法
 */
function queryDepartmentTree(){
    $.ajax({
        url:ROOT_PATH + '/permission/department/getDepartmentTree.shtml',//提交地址
        data:{parentId : 0},//将表单数据序列化
        type:"POST",
        dataType:"json",
        success:function(result){
            //隐患所在部门
            $('#searchDept').combotree(
                {
                    data:result,
                    loadFilter:function(data){
                        var newData = new Array();
                        newData.push({id:"",text:'请选择'});
                        newData.push(data[0]);
                        return newData;
                    }
                }
            );
            //隐患登记部门
            $('#searchAddDept').combotree(
                {
                    data:result,
                    loadFilter:function(data){
                        var newData = new Array();
                        newData.push({id:"",text:'请选择'});
                        newData.push(data[0]);
                        return newData;
                    }
                }
            );

        }
    });
}
/**
 * 人员信息选择
 */
var add_contractorInfo_func = function () {
    WindowManager.openLayer({
        type: 2,
        title: '选择人员信息',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
        callBack:function(data){//窗口关闭后的回调函数
            if(selectUserId==""||selectUserId==null){
                var dutyUserED= datagrid.datagrid('getEditor', {index:editRow,field:'dutyUserName'});
                var dutyUserIdED= datagrid.datagrid('getEditor', {index:editRow,field:'dutyUserId'});
                $(dutyUserED.target).textbox("setValue",data[0].name)
                $(dutyUserIdED.target).textbox("setValue",data[0].id)
            }else{//给正常文本框赋值
                $("#"+selectUserId).textbox('setValue',data[0].id);
                $("#"+selectUserId).textbox('setText',data[0].name);
                selectUserId = "";
            }
        }
    });
};
/**
 * 查询评价方法数据字典
 * @param id
 * @param code
 */
function queryMethodTypeList(id,code){
    $.ajax({
        url:ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml',//提交地址
        data:{code : code},//将表单数据序列化
        type:"POST",
        dataType:"json",
        success:function(result){
            /* var data = [];
             for(i=0;i<result.length;i++){
             if(result[i].data1 == recogniseType || result[i].data1== 2){
             data.push(result[i]);
             }
             }*/
            $('#'+id).combobox(
                {
                    data:result,
                    valueField:'id',
                    textField:'text',
                    loadFilter:function(data){
                        var newData = new Array();
                        newData.push({id:"",text:'请选择'});
                        for(var i=0;i<data.length;i++){
                            newData.push(data[i])
                        }
                        return newData;
                    }
                    /*,
                     onChange:function(newVal,oldVal){
                     if(datagrid){
                     if($("#detailForm").form("validate")){
                     saveTableRow();
                     }else{
                     datagrid.datagrid('deleteRow', editRow);
                     editRow=undefined;
                     }
                     }
                     if(newVal){
                     //$("#detailListDiv").children().show();
                     for(i=0;i<data.length;i++){
                     if(data[i].id==newVal){
                     var tableField = eval(data[i].data2+"_Field").concat(base_Field);
                     tableOptions.columns = [tableField];
                     datagrid = $("#detailList").datagrid(tableOptions);
                     }
                     }
                     }else{
                     $("#detailListDiv").children().hide();
                     }

                     }*/
                }
            );

        }
    });
}